
//注册
kitkatApp.factory('register',function($http) {
	var save = function(user) {
		return $http({
			method:'POST',
			url:'saveRegister.do',
			params:{user:JSON.stringify(user)}
		});
	};
	return {
		save:save
	};
});

//登录
kitkatApp.factory('login',function($http) {
	var save = function(user) {
		return $http({
			method:'POST',
			url:'login.do',
			params:{user:JSON.stringify(user)}
		});
	};
	return {
		save:save
	};
});

//生成uuid
kitkatApp.factory('UUID',function($http) {
	 var getUUID = function () {
	    var s = [];
	    var hexDigits = "0123456789abcdefghijklmnopqrstuvwxyz";
	    for (var i = 0; i < 36; i++) {
	        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	    }
	    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
	    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
	    s[8] = s[13] = s[18] = s[23] = "-";

	    var uuid = s.join("");
	    return uuid;
	}
	return {
		getUUID:getUUID
	};
});

//管理
kitkatApp.factory('manage',function($http,UUID) {
	var getProjects = function() {
		return $http({
			method:'POST',
			url:'getProjects.do'
		});
	};
	var updateProject = function(project) {
		return $http({
			method:'POST',
			url:'updateProject.do',
			params:{
				project:project
			}
		});
	}
	var addProject = function(templateId) {
		var pages = [{
            index:1,
            pageId:UUID.getUUID(),
            entrance:"animated zoomIn",
            exit:"animated zoomOut",
              backImg:{
                src:"lib/images/bg3.jpg",
                style:{
                    position:"absolute",
                    width:300,
                    height:470,
                    left:0,
                    top:0,    
                    opacity:1                    
                }
              },
              backstyle:{
            	background:"#FFF"  
              },
              eles:[]
          }];
		var project = {
				  templateId:templateId,
	              name:"我的KITKAT项目",
	              description:"这是我用KITKAT做的哦，有没有觉得很高大上啊~快来膜拜我",
	              content:pages
		};
		return $http({
			method:'POST',
			url:'addProject.do',
			params:{
				project:project
			}
		});
	};
	var deleteProject = function(projectId) {
		return $http({
			method:'POST',
			url:'deleteProject.do',
			params:{
				projectId:projectId
			}
		});
	}
	return {
		getProjects : getProjects,
		addProject : addProject,
		updateProject : updateProject,
		deleteProject : deleteProject
	};
});


kitkatApp.factory('pageHelper', function($http,$q,UUID) {
    var pageNumber = 0;//页码
    var pages = "";//所有页面数据集合
    var pageLength = 1;//页面个数
    //上一页
    var prevPage = function (){
        if(pageNumber >= 1) {
            //var exit = pages[pageNumber].exit;
            //$("#"+pages[pageNumber].pageId).attr("class",exit);
            pageNumber--;
        }
        return pages[pageNumber];
    };
    //下一页
    var nextPage = function (){
        if(pageNumber + 1 < pages.length) {
            //var exit = pages[pageNumber].exit;
            //$("#"+pages[pageNumber].pageId).attr("class",exit);
            pageNumber++;
        }    
        return pages[pageNumber];
    };
    //初始化
    var getCurrentPage = function() {
        return pages[pageNumber];
    };
    //返回当前页数
    var getCurrentPageNumber = function() {
        return pageNumber;
    };
    //返回总共页数
    var getCurrentpageLength = function() {
        return pageLength;
    };
    //获取某一页内容
    var getPage =function(number) {
        pageNumber = number;
        return pages[pageNumber];
    };
    //保存页面信息
    var save = function(data){
        pages = data;
        pageLength = pages.length;
    };
    //获取所有页面数据
    var init = function(projectId) {
    		/*var pages = [{
                index:1,
                pageId:UUID.getUUID(),
                entrance:"animated zoomIn",
                exit:"animated zoomOut",
                  backImg:{
                    src:"lib/images/bg3.jpg",
                    style:{
                        position:"absolute",
                        width:300,
                        height:470,
                        left:0,
                        top:0,    
                        opacity:1                    
                    }
                  },
                  backstyle:{
                	background:"#FFF"  
                  },
                  eles:[]
              }];
    		var deferred = $q.defer();
    		setTimeout(function(){
    			deferred.resolve(pages);
    		},1000);
    	return deferred.promise;*/
    	return $http({
			method:'POST',
			url:'getProjectById.do',
			params:{
				projectId:projectId
			}
		});
    		
    };
    return {
        init:init,
        save:save,
        getPage:getPage,
        prevPage:prevPage,
        nextPage:nextPage,
        getCurrentPageNumber:getCurrentPageNumber,
        getCurrentpageLength:getCurrentpageLength
    };
});  